Date: 09/25/2019
Data source
Developer Information
GitHub
This is second part of a two-part series on accessing the International Debt Statistics (IDS) database through the World Bank Data API. In Part 1, we queried the World Bank Data API to retrieve indicator names and location codes. In this guide, we will use that information to explore the regional trends of long-term external debt stocks from the IDS database.
The following code in this guide will show step-by-step how to:
To start, make sure you have the following packages installed on your machine. To install an R package type install.packages(“WDI”) with the correct package name into R. You can also visit each of the linked packages below for reference.
Then, open up your preferred mode of writing R, like R Studio. Now follow the rest of the steps below to retreive and analyze the IDS data.
# Load packages
library(WDI)
library(ggplot2)
library(plotly)
You will specify the the data that you want to explore using the following parameters:
In this guide, we will be looking at “long-term external debt stock” from the IDS database. To find the indicator for the data in which you’re interested, you can either explore the World Bank data catalog or use an API query as outlined in Part 1 of this series. The IDS indicators are also conveniently stored as a spreadsheet (LINK NEEDED) in this repo.
# Selecting the indicator
dataSeries = "DT.DOD.DLXF.CD"
To select a location by country, region, or income level category you will need to know its 2 or 3 letter code. To figure out what this code is, you can either use an API query as outlined in Part 1 of this series or use the convenient location-codes csv* in this repo.
We will select regional aggregates (these exclude high-income countries):
# Select the countries or regions
location = c("ECA","SSA","SAS","LAC","MNA","EAP")
Here you will select the time frame for the data you are retrieving, by picking the first and last year of the time frame.
# Selecting the time frame
firstYear = 2008
lastYear = 2018
In this step, we will retrieve the data using the World Bank Data API call using the R package “WDI.” In the following code, we use the parameters selected above to request our data.
data = WDI(indicator=dataSeries, country=location, start=firstYear, end=lastYear)
If you want a quick preview of your freshly retrieved DataFrame, you can print the first 5 lines
head(data)
## iso2c country DT.DOD.DLXF.CD year
<<<<<<< Updated upstream
## 1 4E East Asia & Pacific (excluding high income) 1.285327e+12 2017
## 2 4E East Asia & Pacific (excluding high income) 1.172696e+12 2016
## 3 4E East Asia & Pacific (excluding high income) 1.036149e+12 2015
## 4 4E East Asia & Pacific (excluding high income) 1.040363e+12 2014
## 5 4E East Asia & Pacific (excluding high income) 8.555972e+11 2013
## 6 4E East Asia & Pacific (excluding high income) 7.831267e+11 2012
=======
## 1 4E East Asia & Pacific (excluding high income) 1.391850e+12 2018
## 2 4E East Asia & Pacific (excluding high income) 1.285327e+12 2017
## 3 4E East Asia & Pacific (excluding high income) 1.172696e+12 2016
## 4 4E East Asia & Pacific (excluding high income) 1.036149e+12 2015
## 5 4E East Asia & Pacific (excluding high income) 1.040363e+12 2014
## 6 4E East Asia & Pacific (excluding high income) 8.555972e+11 2013
>>>>>>> Stashed changes
Congratulations! At this point you should have the long-term external debt stock for regions (excluding high-income economies) from 2008 - 2017 all in a DataFrame called “data.”
Now we can do the following:
As you saw in the preview of the data in section 3, the data needs some basic cleaning.
The data for the long-term external debt stock is currently in units. To improve a table’s or chart’s readability, convert the units to billions and change the visible decimal places to zero.
# change units to billions
data$DT.DOD.DLXF.CD = data$DT.DOD.DLXF.CD/1000000000
# hide decimal places
data$DT.DOD.DLXF.CD = round(data$DT.DOD.DLXF.CD, 0)
These next sections of code will clean up the naming of headers and regions. First, it will rename the column headers. Second, it will remove the redundant “(excluding high income)” from the region names. We can instead include that information in the title of the legend. Finally, it will remove any unneeded columns.
# Rename column headers
colnames(data)[colnames(data)=="country"] <- "Region"
colnames(data)[colnames(data)=="DT.DOD.DLXF.CD"] <- "ExternalDebtStock"
colnames(data)[colnames(data)=="year"] <- "Year"
# Remove "(excluding high income)" from each of the region names
data$Region <- gsub("excluding high income", "", data$Region)
# Remove the parenthesis
data$Region <- gsub("\\()", "", data$Region)
# Remove the iso2c column
data <- subset(data, select = -c(iso2c))
Now our data should be ready to present in a table or visualize. Let’s take a look at the first five lines again so we can compare the cleaned up data to the output in section 3.
head(data)
## Region ExternalDebtStock Year
<<<<<<< Updated upstream
## 1 East Asia & Pacific 1285 2017
## 2 East Asia & Pacific 1173 2016
## 3 East Asia & Pacific 1036 2015
## 4 East Asia & Pacific 1040 2014
## 5 East Asia & Pacific 856 2013
## 6 East Asia & Pacific 783 2012
=======
## 1 East Asia & Pacific 1392 2018
## 2 East Asia & Pacific 1285 2017
## 3 East Asia & Pacific 1173 2016
## 4 East Asia & Pacific 1036 2015
## 5 East Asia & Pacific 1040 2014
## 6 East Asia & Pacific 856 2013
>>>>>>> Stashed changes
Now use the package “Plotly” to create a line graph, similar to one from the blog post on the launch of IDS 2019.
p<-plot_ly(
data, x = ~Year, y=~ExternalDebtStock,
type="scatter", mode = "lines",
hoverinfo = "text",
text=~paste("External Debt Stock: ",ExternalDebtStock, "<br>Region: ",Region, "<br>Year: ",Year),
color=~Region)%>%
layout(
title= "Long-term External Debt Stock (USD billion)"
)
p
<<<<<<< Updated upstream
=======
>>>>>>> Stashed changes